<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 购物车
 */

namespace app\api\controller\user\v1_0;

use AlibabaCloud\SDK\OSS\OSS\SelectObjectRequest\body\selectRequest;

class Cart extends Common
{
    protected $noNeedRight = ['index','cart'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 默认接口
     */    
    public function index(){
        $data = (new \app\common\model\AgentGoodsCart())
                ->alias('cart')
                ->join("{$this->tdb}agent_goods_sku agent_goods_sku","agent_goods_sku.id = cart.goods_sku_id")
                ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                ->join("{$this->tdb}goods_sku goods_sku","goods_sku.id = agent_goods_sku.goods_sku_id")
                ->where([
                    'cart.user_id' => $this->UserInfo['id'],
                    'cart.agent_id' => $this->agent_id,
                    'cart.bus_id' => $this->bus_id,
                    ]) 
                    ->where([
                        'agent_goods_sku.is_display' => 1,
                        'goods.is_display' => 1,
                    ])
                ->field([
                    'agent_goods_sku.id',
                    'goods.img',
                    'goods.title',
                    'goods.introduce',
                    'agent_goods_sku.money',
                    'agent_goods_sku.money_primary',
                    'cart.num',
                    'goods_sku.stock'
                ])->select();
        return self::successResult("成功",$data);      
    }
    public function num(){
        $data = (new \app\common\model\AgentGoodsCart())
        ->alias('cart')
        ->join("{$this->tdb}agent_goods_sku agent_goods_sku","agent_goods_sku.id = cart.goods_sku_id")
        ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
        ->where([
            'cart.user_id' => $this->UserInfo['id'],
            'cart.agent_id' => $this->agent_id,
            'cart.bus_id' => $this->bus_id,
            ]) 
            ->where([
                'agent_goods_sku.is_display' => 1,
                'goods.is_display' => 1,
            ])
        ->sum('num');
        return self::successResult("成功",$data);      
    }
      /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 加入购物车
     */ 
    public function add(){
        $param = request()->param();
  
        $cart = (new \app\common\model\AgentGoodsCart())->where([
            'goods_sku_id' => $param['sku_id'],
            'user_id' => $this->UserInfo['id'],
            'bus_id' => $this->bus_id,
            'agent_id' => $this->agent_id,
        ])->find();
        #第一次加入购物
        $sku = (new \app\common\model\AgentGoodsSku())->where(['id' => $param['sku_id']])->find();
        if($sku['stock'] < $param['num']){
            return self::errorResult("库存不足");     
        }
        if(empty($cart)){
            (new \app\common\model\AgentGoodsCart())->insert([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
                'num' => $param['num'],
            ]);
        }else{
            (new \app\common\model\AgentGoodsCart())->where([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
                'num' => $cart['num']
            ])->update([
                'num' => $cart['num']+$param['num'],
            ]);
        }
        return self::successResult("成功",[]);     
    }
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 购物车数量增减
     */ 
    public function cart(){
        $param = request()->param();
        $cart = (new \app\common\model\AgentGoodsCart())->where([
            'goods_sku_id' => $param['sku_id'],
            'user_id' => $this->UserInfo['id'],
            'bus_id' => $this->bus_id,
            'agent_id' => $this->agent_id,
        ])->find();
        if(empty($cart)){
            return self::errorResult("购物车商品不存在！请重新添加",[]);  
        }
        $sku = (new \app\common\model\AgentGoodsSku())->where(['id' => $param['sku_id']])->find();
        if($sku['stock'] < $param['num']){
            return self::errorResult("库存不足");     
        }
        #购物车数量加
        if($param['type'] == 1){
            (new \app\common\model\AgentGoodsCart())->where([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
                'num' => $cart['num']
            ])->update([
                'num' => $cart['num']+$param['num'],
            ]);
        }
        #购物车数量加
        if($param['type'] == 0){
            if($cart['num']-$param['num'] < 1){
                (new \app\common\model\AgentGoodsCart())->where([
                    'goods_sku_id' => $param['sku_id'],
                    'user_id' => $this->UserInfo['id'],
                    'bus_id' => $this->bus_id,
                    'agent_id' => $this->agent_id,
                    'num' => $cart['num']
                ])->delete();
                return self::successResult("成功",[]); 
            }
            (new \app\common\model\AgentGoodsCart())->where([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
                'num' => $cart['num']
            ])->update([
                'num' => $cart['num']-$param['num'],
            ]);
        }
        return self::successResult("成功",[]);   
    }
  public function newcart(){
        $param = request()->param();
        $cart = (new \app\common\model\AgentGoodsCart())->where([
            'goods_sku_id' => $param['sku_id'],
            'user_id' => $this->UserInfo['id'],
            'bus_id' => $this->bus_id,
            'agent_id' => $this->agent_id,
        ])->find();
        #第一次加入购物
        $sku = (new \app\common\model\AgentGoodsSku())->where(['id' => $param['sku_id']])->find();
        if(($sku['stock'] < $param['num']) ){
            return self::errorResult("库存不足");     
        }
        if($param['num'] == 0){
            $sku = (new \app\common\model\AgentGoodsCart())->where([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
            ])->delete(); 
            return self::successResult("删除成功");    
        }
        if($param['num'] < 1){
            return self::errorResult("不可以输入负数的哟！！！");    
        }
        if(empty($cart)){
            (new \app\common\model\AgentGoodsCart())->insert([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
                'num' => $param['num'],
            ]);
        }else{
            
            (new \app\common\model\AgentGoodsCart())->where([
                'goods_sku_id' => $param['sku_id'],
                'user_id' => $this->UserInfo['id'],
                'bus_id' => $this->bus_id,
                'agent_id' => $this->agent_id,
                'num' => $cart['num']
            ])->update([
                'num' => $param['num'],
            ]);
        }
        
        return self::successResult("成功",[]);   
  }
 
    
}
